unit setitem;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  StdCtrls, ComCtrls, Buttons, clientdb;

type

  { TSetItemForm }

  TSetItemForm = class ( TForm )
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    ServicesList: TListView;
    SelectPageControl: TPageControl;
    ButtonPanel: TPanel;
    ServicesTab: TTabSheet;
    ProductsTab: TTabSheet;
    ProductTree: TTreeView;
    procedure Button1Click ( Sender: TObject ) ;
    procedure FormCreate ( Sender: TObject ) ;
  private
    { private declarations }
  public
    { public declarations }
    procedure GetProducts;
    procedure GetServices;
  end;

var
  SetItemForm: TSetItemForm;

implementation

uses main;

{ TSetItemForm }

procedure TSetItemForm.FormCreate ( Sender: TObject ) ;
begin
  GetServices;
  GetProducts;
end;

procedure TSetItemForm.Button1Click ( Sender: TObject ) ;
begin
  ShowMessage(IntToStr(Integer(ServicesList.Selected.Data)));
end;

procedure TSetItemForm.GetProducts;
var
  suppliernode: TTreeNode;
  supplier: String;
  node: TTreeNode;
begin
  MainDB.SQL := 'SELECT productid, Suppliers.name AS Supplier, Products.name AS Product, saleprice FROM Products, Suppliers WHERE Suppliers.id=Products.suppliercode ORDER BY Supplier, Product;';
  MainDB.Open;
  MainDB.First;
  While not MainDB.EOF do
        begin
          supplier := MainDB.FieldByName('Supplier').AsString;
          suppliernode := ProductTree.Items.Add(nil, supplier);
          while (not MainDB.EOF) and (supplier=MainDB.FieldByName('Supplier').AsString) do
                begin
                  node := ProductTree.Items.AddChild(suppliernode, MainDB.FieldByName('Product').AsString);
                  node.Data := Pointer(MainDB.FieldByName('productid').AsInteger);
                  MainDB.Next;
                end;
        end;
  MainDB.Close;
end;

procedure TSetItemForm.GetServices;
var
  suppliernode: TTreeNode;
  supplier: String;
  node: TTreeNode;
  li: TListItem;
begin
  MainDB.SQL := 'SELECT productid, Products.name AS Service, saleprice FROM Products WHERE productid>0 AND productid<1000 ORDER BY Service;';
  MainDB.Open;
  MainDB.First;
  While not MainDB.EOF do
        begin
          li := ServicesList.Items.Add;
          li.Caption := MainDB.FieldByName('Service').AsString;
          li.SubItems.Add(MainDB.FieldByName('saleprice').AsString);
          li.Data := Pointer(MainDB.FieldByName('productid').AsInteger);
          MainDB.Next;
        end;
  MainDB.Close;
end;

{$R *.lfm}

end.

